



import java.awt.image.*;
public class PixelAnh
{
    private BufferedImage myImage;
    private int width;
    private int height;
    

    public int getHeight()
    {
        return this.height;
    } 
    
    public int getWidth()
    {
        return this.width;
    }
    
   
    
    public PixelAnh(BufferedImage VungDem)   
    {
        this.myImage = VungDem;
        this.width = VungDem.getWidth();
        this.height = VungDem.getHeight();
    }
   public void setData(Pixel[][] data)
    {
        int[] pixelValues = new int[3];     
        WritableRaster wr = this.myImage.getRaster();
        
        if (data.length != wr.getHeight())
        {
            throw new IllegalArgumentException("Array size does not match");
        }
        else if (data[0].length != wr.getWidth())
        {
            throw new IllegalArgumentException("Array size does not match");
        }
        
        for (int row = 0; row < wr.getHeight(); row++)
        {
            for (int col = 0; col < wr.getWidth(); col++)
            {
                pixelValues[0] = data[row][col].red;
                pixelValues[1] = data[row][col].green;
                pixelValues[2] = data[row][col].blue;
                wr.setPixel(col, row, pixelValues);
            }
        }
    }
    
    
  
    public Pixel[][] getData()
    {
        Raster r = this.myImage.getRaster();
        Pixel[][] data = new Pixel[r.getHeight()][r.getWidth()];
        int[] samples = new int[3];
        
        for (int row = 0; row < r.getHeight(); row++)
        {
            for (int col = 0; col < r.getWidth(); col++)
            {
                samples = r.getPixel(col, row, samples);
                Pixel newPixel = new Pixel(samples[0], samples[1], samples[2]);
                data[row][col] = newPixel;
            }
        }
        
        return data;
    }
     public BufferedImage getImage()
    {
        return this.myImage;
    }

    

}
